import Vue from 'vue'
import Router from 'vue-router'
import login from '@/view/login/index'
import mainpage from '@/view/mainpage/index'
import page1 from '@/view/page/page1'
//
import myinfo from '@/view/page/student/myinfo'
import myDeliver from '@/view/page/student/myDeliver'
import jobCenter from '@/view/page/student/jobCenter'
import companyCenter from '@/view/page/student/companyCenter'
//
import companyinfo from '@/view/page/company/companyinfo'
import companyjob from '@/view/page/company/companyjob'
import jobform from '@/view/page/company/jobform'
import peocenter from '@/view/page/company/peocenter'
import recordcenter from '@/view/page/company/recordcenter'
//
import infoManagement from '@/view/page/admin/infoManagement'
import jobsManagement from '@/view/page/admin/jobsManagement'
import recordManagement from '@/view/page/admin/recordManagement'
import userManagement from '@/view/page/admin/userManagement'



import {
  cookieData
} from "@/util/local";


Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'mainpage',
      component: mainpage,
      children:[
        {
          path: '/',
          name: 'page1',
          component: page1
        },{
          path: '/myinfo',
          name: 'myinfo',
          component: myinfo
        },{
          path: '/myDeliver',
          name: 'myDeliver',
          component: myDeliver
        },{
          path: '/jobCenter',
          name: 'jobCenter',
          component: jobCenter
        },{
          path: '/companyCenter',
          name: 'companyCenter',
          component: companyCenter
        },{
          path: '/companyinfo',
          name: 'companyinfo',
          component: companyinfo
        },{
          path: '/companyjob',
          name: 'companyjob',
          component: companyjob
        },{
          path: '/jobform',
          name: 'jobform',
          component: jobform
        },{
          path: '/peocenter',
          name: 'peocenter',
          component: peocenter
        },{
          path: '/recordcenter',
          name: 'recordcenter',
          component: recordcenter
        },{
          path: '/infoManagement',
          name: 'infoManagement',
          component: infoManagement
        },{
          path: '/jobsManagement',
          name: 'jobsManagement',
          component: jobsManagement
        },{
          path: '/recordManagement',
          name: 'recordManagement',
          component: recordManagement
        },{
          path: '/userManagement',
          name: 'userManagement',
          component: userManagement
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: login
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
    if (!cookieData("get", "token")) {
      next()
    } else {
      next("/");
    }
  } else {
    if (cookieData("get", "token")) {
      next()
    } else {
      next("/login");
    }
  }
})

export default router
